package com.wing.robot.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.robot.dao.RobotAgvInfoDao;
import com.wing.robot.model.entity.RobotAgvInfo;
import com.wing.robot.model.request.RobotAgvInfoForm;
import com.wing.robot.model.response.RobotAgvInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机械臂协作机器人本身仓储位置信息-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class RobotAgvInfoServiceImpl extends ServiceImpl<RobotAgvInfoDao, RobotAgvInfo> implements RobotAgvInfoService {

    @Override
    public void add(RobotAgvInfoForm robotAgvInfoForm) throws Exception {

        //校验
        //如果该agv已有初始位置信息 不让添加
        if (robotAgvInfoForm.getInit().equals(RobotAgvInfo.Init.init)){
            List<RobotAgvInfoVO> list = baseMapper.list4app(new HashMap<String,Object>(){{put("agvId",robotAgvInfoForm.getAgvId());}});
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getInit().equals(RobotAgvInfo.Init.init)){
                    throw new Exception("该AGV已存在默认初始位置");
                }
            }
        }
        RobotAgvInfo robotAgvInfo =new RobotAgvInfo();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(RobotAgvInfo.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(robotAgvInfoForm, robotAgvInfo, copyOptions);
        robotAgvInfo.setStatus(RobotAgvInfo.Status.idle);
        baseMapper.insert(robotAgvInfo);
    }

    @Override
    public List<RobotAgvInfoVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<RobotAgvInfoVO> pageList4app(Page page, Map<String, Object> params) {
        List<RobotAgvInfoVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public RobotAgvInfoVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int updateStatus(Long id, RobotAgvInfo.Status status) {
        RobotAgvInfo robotAgvInfo = new RobotAgvInfo();
        robotAgvInfo.setId(id);
        robotAgvInfo.setStatus(status);
        return baseMapper.updateById(robotAgvInfo);
    }
}
